import type { RouteRecordNormalized } from 'vue-router';

type RouteModule = {
  default: RouteRecordNormalized | RouteRecordNormalized[];
};
type RouteModules = Record<string, RouteModule>;

const modules = import.meta.glob('./modules/*.ts', {
  eager: true,
}) as RouteModules;
function formatModules(
  _modules: RouteModules,
  result: RouteRecordNormalized[],
) {
  Object.keys(_modules).forEach((key: string) => {
    const defaultModule = _modules[key].default;
    if (!defaultModule) return;
    const moduleList = Array.isArray(defaultModule)
      ? [...defaultModule]
      : [defaultModule];
    result.push(...moduleList);
  });
  return result;
}

export const AppRoutes: RouteRecordNormalized[] = formatModules(modules, []);
